package org.bigjava.controller;

import org.bigjava.bean.Wuye;
import org.bigjava.biz.WuyeService;
import org.bigjava.common.Constant;
import org.bigjava.common.FenYe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.xml.crypto.KeySelector;
import java.util.List;

@Controller
public class WuyeController {

    @Autowired
    HttpSession session;
    @Autowired
    WuyeService wuyeService;

    //搜索物业
    @RequestMapping("wuye/sousuo")
    public String wuYeSouSuo(String dyg,String deg){
        List<Wuye> lists = wuyeService.wuYeMoHu(dyg,deg);
        session.setAttribute("wuyes",lists);
        return "wygl";
    }

    //添加物业
    @RequestMapping("wuye/tianjia")
    @ResponseBody
    public String wuYeAdd(Wuye wuye){
        int i = wuyeService.wuYeAdd(wuye);
        if(i>0){
            return "OK";
        }else{
            return "添加失败";
        }
    }

    //修改物业
    @RequestMapping("wuye/xiugai")
    @ResponseBody
    public String wuYeXiuGai(Wuye wuye){
        Wuye wuyea = (Wuye) session.getAttribute("wuye");
        wuye.setId(wuyea.getId());
        int i = wuyeService.wuYeXiuGai(wuye);
        if(i>0){
            return "OK";
        }else{
            return "修改失败";
        }
    }

    //物业账号校验
    @RequestMapping("wuye/jiaoyan")
    @ResponseBody
    public Wuye wuYeJiaoYan(String username){
        Wuye wuye = wuyeService.wuYeJiaoYan(username);
        Wuye wuyea = (Wuye) session.getAttribute("wuye");
        if(wuye!=null){
            if(wuye.getId()==wuyea.getId()){
                return null;
            }
        }
        return wuye;
    }

    //跳转到物业修改页面
    @RequestMapping("wuye/xg")
    public String wuYeXiuGai(int id){
        Wuye wuye = wuyeService.wuYeId(id);
        session.setAttribute("wuye",wuye);
        return "wyxg";
    }

    //删除物业
    @RequestMapping("wuye/shanchu")
    public String wuYeShanChu(int id){
        wuyeService.wuYeShanChu(id);
        //拿到当前页数
        Object a = session.getAttribute("yeshu");
        return "redirect:/wuye/fenye?a="+a;
    }


    //物业分页查询
    @RequestMapping("wuye/fenye")
    public String wuYeFenYe(String a){
        //业主总人数
        int zong =  wuyeService.wuYeZong();
        session.setAttribute("zong",zong);
        //调用分页方法返回下一页开始id
        int ks = FenYe.fenYe(session,zong,a, Constant.fenye);
        //分页查询出的业主
        List<Wuye> lists = wuyeService.wuYeFenYe(ks,Constant.fenye);
        session.setAttribute("wuyes",lists);
        return "wygl";
    }

    //物业登录
    @RequestMapping("wuye/denglu")
    @ResponseBody
    public String wuYeDengLu(String username,String password){
        Wuye wuye = wuyeService.wuyeDengLu(username,password);
        if(wuye == null){
            return "登录失败";
        }else{
            session.setAttribute("wuye",wuye);
            return "/wuye.html";
        }
    }

}
